package com.lagou.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerInitClose {

    /**
     * 自定义成员变量记录ServerSocket, Socket
     */



    private ServerSocket ss;
    private Socket s;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    public ObjectInputStream getOis() {
        return ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    /**
     * 自定义初始化 服务器初始化
     */

    public void serverInit() throws IOException {

        //1. ServerSocket 对象 端口号
        ss = new ServerSocket(9899);

        //2. 等待连接
        System.out.println("等待客户端连接请求");
        s = ss.accept();
        System.out.println("客户端连接成功");

        //3.输入输出流
        ois = new ObjectInputStream(s.getInputStream());
        oos = new ObjectOutputStream(s.getOutputStream());
        System.out.println("服务器初始化成功");



    }
    //4.关闭Socket

    public void serverClose()  {
        if (null != oos) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != ois) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != s) {
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != ss) {
            try {
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("服务器成功关闭");
    }
}
